// EPropertyTextblock.cpp: implementation of the EPropertyTextblock class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "others/EPropertyTextblockWindow.h"
#include "IPropertyHost.h"
#include "EPropertyTextblock.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

EPropertyTextblock::EPropertyTextblock(IPropertyHost *pHost, const std::wstring &sName, std::wstring *pProperty, bool bEnabled)
    : EIconTextButtonProperty(pHost, sName, pProperty, bEnabled)
    , m_pPropertyEditWindow(nullptr)
    , m_pCtrl(nullptr)
{}

EPropertyTextblock::~EPropertyTextblock()
{
    CloseEditWindow();
}

bool EPropertyTextblock::HasButton()
{
    return true;
}

std::wstring EPropertyTextblock::GetTextValue()
{
    std::wstring s = *(std::wstring *)m_pProperty;

    std::wstring t;
    int n = static_cast<int>(s.length());

    for(int i = 0; i < n; ++i) {
        auto c = s[i];

        if(c == L'\r') {
            //			t += "\\r";
            t += L"<>";
        } else if(c == L'\n') {
            //			t += "\\n";
            t += L"<>";
        } else {
            t += c;
        }
    }

    return t;
}

bool EPropertyTextblock::OnCommand(EPropWnd *pParent, WPARAM wParam, LPARAM lParam)
{
    bool bPropertyChanged = false;

    m_pCtrl = pParent;

    if(!m_pPropertyEditWindow) {
        CPoint ButtonCenter = m_ButtRect.CenterPoint();
        pParent->ClientToScreen(&ButtonCenter);

        CRect r(0, 0, 200, 200);
        r.OffsetRect(ButtonCenter.x, ButtonCenter.y);

        int scrw = GetSystemMetrics(SM_CXSCREEN);
        int scrh = GetSystemMetrics(SM_CYSCREEN);
        if(ButtonCenter.x >= scrw / 2) {
            r.OffsetRect(-200, 0);
        }

        if(ButtonCenter.y >= scrh / 2) {
            r.OffsetRect(0, -200);
        }

        m_pPropertyEditWindow = std::make_unique<EPropertyTextblockWindow>(this, pParent);
        m_pPropertyEditWindow->Create(NULL, m_sName.c_str(), WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME | WS_VISIBLE, r, pParent);
        m_pPropertyEditWindow->SetEditText(*((std::wstring *)m_pProperty));
    }

    return bPropertyChanged;
}

void EPropertyTextblock::OnWindowButtonOk()
{
    m_pCtrl->OnDoPropApply();
}

void EPropertyTextblock::OnWindowButtonCancel()
{
    CloseEditWindow();
}

void EPropertyTextblock::CloseEditWindow()
{
    if(m_pPropertyEditWindow && m_pPropertyEditWindow->GetSafeHwnd()) {
        m_pPropertyEditWindow->DestroyWindow();
        m_pPropertyEditWindow.reset();
    }
}

bool EPropertyTextblock::OnPropApply()
{
    bool bChangeOK = false;

    if(m_pPropertyEditWindow) {
        std::wstring s = m_pPropertyEditWindow->GetEditText();

        bChangeOK = m_pHost->PropertyChanging(m_pProperty, &s);

        if(bChangeOK) {
            *((std::wstring *)m_pProperty) = s;
        }

        CloseEditWindow();
    }

    return bChangeOK;
}

void EPropertyTextblock::OnPropClose()
{
    EIconTextButtonProperty::OnPropClose();
    OnWindowButtonCancel();
}